package com.zy.garbage.service.impl;

import com.zy.garbage.entity.*;
import com.zy.garbage.exception.CommonException;
import com.zy.garbage.form.ExamSimpleForm;
import com.zy.garbage.form.QuestionAnswerForm;
import com.zy.garbage.repository.*;
import com.zy.garbage.service.QuestionService;
import com.zy.garbage.util.MyDateUtil;
import com.zy.garbage.vo.ExamVO;
import com.zy.garbage.vo.QuestionListVO;
import com.zy.garbage.vo.QuestionStatisticVO;
import com.zy.garbage.vo.QuestionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private GarbageRepository garbageRepository;
    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private SortRepository sortRepository;
    @Autowired
    private UserRepository userRepository;


    @Override
    public QuestionListVO save(Integer examId) {
        HashSet<Integer> set = new HashSet<>();
        List<Garbage> garbageList = garbageRepository.findAll(org.springframework.data.domain.Sort.by(org.springframework.data.domain.Sort.Direction.DESC,"garbageId"));
        Integer max = garbageList.get(0).getGarbageId();
        QuestionListVO questionListVO = new QuestionListVO();
        List<QuestionVO> questionVOList = new ArrayList<>();
        if (!examRepository.findById(examId).isPresent()){
            throw new CommonException(500,"问卷不存在");
        }
        Exam exam = examRepository.findById(examId).get();
        ExamVO examVO = new ExamVO();
        examVO.setScore(0);
        Integer userId = exam.getUserId();
        User user = userRepository.findById(userId).get();
        examVO.setUserPhone(user.getUserPhone());
        examVO.setUserName(user.getUserName());
        BeanUtils.copyProperties(exam,examVO);
//        examVO.setCreateTime(MyDateUtil.date2String(exam.getCreateTime()));
        questionListVO.setExamVO(examVO);
        while (set.size()!=10){
            Random random = new Random();
            Integer garbageId = random.nextInt(max+1);
            if (garbageRepository.findById(garbageId).isPresent()){
                set.add(garbageId);
            }
        }
        Integer questionSn = 0;
        for (Integer garbageId : set) {
            Garbage garbage = garbageRepository.findById(garbageId).get();
            Integer sortId = garbage.getSortId();
            Sort sort = sortRepository.findById(sortId).get();
            Question question = new Question();
            question.setExamId(examId);
            question.setGarbageId(garbageId);
            question.setSortId(garbage.getSortId());
            Question question1 = questionRepository.save(question);
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question,questionVO);
            BeanUtils.copyProperties(garbage,questionVO);
            questionVO.setQuestionSn(++questionSn);
            questionVO.setQuestionId(question1.getQuestionId());
            questionVO.setSortInfo(sort.getSortInfo());
            questionVO.setSortName(sort.getSortName());
            questionVOList.add(questionVO);
        }
        questionListVO.setQuestionVOList(questionVOList);
        return questionListVO;
    }

    @Override
    public QuestionListVO findByExamId(Integer examId) {
        if (!examRepository.findById(examId).isPresent()){
            throw new CommonException(500,"问卷不存在");
        }
        List<Sort> sortList = sortRepository.findAll();
        Map<Integer,String> hashMap = new HashMap<>();
        for (Sort sort : sortList) {
            hashMap.put(sort.getSortId(),sort.getSortName());
        }
        QuestionListVO questionListVO = new QuestionListVO();
        List<QuestionVO> questionVOList = new ArrayList<>();
        Exam exam = examRepository.findById(examId).get();
        ExamVO examVO = new ExamVO();
        BeanUtils.copyProperties(exam,examVO);
        examVO.setCreateTime(MyDateUtil.date2String(exam.getCreateTime()));
        List<Question> questionList = questionRepository.findAllByExamId(examId);
        Integer questionSn = 0;
        Integer score = 0;
        for (Question question : questionList) {
            Integer garbageId = question.getGarbageId();
            Garbage garbage = garbageRepository.findById(garbageId).get();
            Integer sortId = garbage.getSortId();
            Sort sort = sortRepository.findById(sortId).get();
            question.setExamId(examId);
            question.setGarbageId(garbageId);
            question.setSortId(garbage.getSortId());
            Question question1 = questionRepository.save(question);
            QuestionVO questionVO = new QuestionVO();
            BeanUtils.copyProperties(question,questionVO);
            BeanUtils.copyProperties(garbage,questionVO);
            questionVO.setQuestionSn(++questionSn);
            questionVO.setQuestionId(question1.getQuestionId());
            questionVO.setSortInfo(sort.getSortInfo());
            questionVO.setSortName(sort.getSortName());
            questionVO.setAnswerName(hashMap.get(question.getAnswerId()));
            questionVOList.add(questionVO);
            if (questionVO.getAnswerId()==questionVO.getSortId()){
                score += 10;
            }
        }
        exam.setScore(score);
        examRepository.save(exam);
        examVO.setScore(score);
        questionListVO.setExamVO(examVO);
        questionListVO.setQuestionVOList(questionVOList);
        return questionListVO;
    }

    @Override
    public QuestionListVO update(ExamSimpleForm examSimpleForm) {
        List<QuestionAnswerForm> questionAnswerFormList = examSimpleForm.getQuestion();
        for (QuestionAnswerForm questionAnswerForm : questionAnswerFormList) {
            Integer questionId = questionAnswerForm.getQuestionId();
            if (questionRepository.findById(questionId).isPresent()){
                Question question = questionRepository.findById(questionId).get();
                question.setAnswerId(questionAnswerForm.getAnswerId());
                questionRepository.save(question);
            }
        }
        return findByExamId(examSimpleForm.getExam());
    }

    @Override
    public List<QuestionStatisticVO> statistic() {
        List<Sort> sortList = sortRepository.findAll();
        HashMap<Integer,Sort> sortMap = new HashMap<>();
        for (Sort sort : sortList) {
            sortMap.put(sort.getSortId(),sort);
        }

        HashMap<Integer,String> garbageHashMap = new HashMap<>();
        List<Garbage> garbageList = garbageRepository.findAll();
        for (Garbage garbage : garbageList) {
            garbageHashMap.put(garbage.getGarbageId(),garbage.getGarbageName());
        }

        List<QuestionStatisticVO> questionStatisticVOList = new ArrayList<>();
        List<Integer> distinctList =  questionRepository.findAllGroupByGarbageId();
        for (Integer garbageId : distinctList) {
            if (questionRepository.findAllByGarbageId(garbageId).size()==0){
                break;
            }
            QuestionStatisticVO questionStatisticVO = new QuestionStatisticVO();
            questionStatisticVO.setGarbageId(garbageId);
            String garbageName = garbageHashMap.get(garbageId);
            questionStatisticVO.setGarbageName(garbageName);
            List<Question> questionList = questionRepository.findAllByGarbageId(garbageId);
            Question question = questionList.get(0);
            Integer sortId = question.getSortId();
            Sort sort = sortMap.get(sortId);
            BeanUtils.copyProperties(sort,questionStatisticVO);
            Integer all = questionList.size();
            questionStatisticVO.setAll(all);
            Integer yes = questionRepository.findYesByGarbageId(garbageId);
            questionStatisticVO.setYes(yes);
            Integer no = questionRepository.findNoByGarbageId(garbageId);
            questionStatisticVO.setNo(no);
            Integer none = questionRepository.findNoneByGarbageId(garbageId);
            questionStatisticVO.setNone(none);
            questionStatisticVOList.add(questionStatisticVO);
        }
        return questionStatisticVOList;
    }
}
